//
//  TJVisitorView.swift
//  weibo
//
//  Created by 简祖明 on 16/7/27.
//  Copyright © 2016年 汤娟. All rights reserved.
//

import UIKit
import SnapKit

//定义协议
protocol TJVisitorViewDelegate : NSObjectProtocol {
    //注册按钮的方法
    func loginMethod()
    //登录按钮的方法
    func registerMethod()
}


class TJVisitorView: UIView {
    
    //定义代理属性
    weak var delegate : TJVisitorViewDelegate?

    //1.重写父类的构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        //添加子控件
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //2.添加子类和设置约束
    private func setupUI(){
        //设置背景色
        backgroundColor = UIColor(white: 237/255.0, alpha: 1.0)
        
        //添加到视图(蒙板在房子图片的下面，转动图片的上面)
        addSubview(turnImageView)
        addSubview(coverImageView)
        addSubview(homeImageView)
        addSubview(messageLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        //设置约束
        //小房子图片
        homeImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        
        //蒙板
        coverImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        
        //转动图片
        turnImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        
        //先水平，后垂直，再上下左右，再宽高
        //文本内容
        messageLabel.snp_makeConstraints { (make) -> Void in
           make.centerX.equalTo(self.snp_centerX)
            make.top.equalTo(turnImageView.snp_bottom).offset(10)
            make.width.equalTo(216)
            
        //注册按钮
            registerBtn.snp_makeConstraints(closure: { (make) -> Void in
                make.left.equalTo(messageLabel.snp_left)
                make.top.equalTo(messageLabel.snp_bottom).offset(10)
                make.width.equalTo(100)
                make.height.equalTo(30)
            })
            
        //登录按钮
            loginBtn.snp_makeConstraints(closure: { (make) -> Void in
                make.right.equalTo(messageLabel.snp_right)
                make.top.equalTo(messageLabel.snp_bottom).offset(10)
                make.width.equalTo(100)
                make.height.equalTo(30)
            })
        }
        
    }
    
    //MARK : - 各个控制器来改变视图的方法
    //isHome:false默认不是首页
    func setupInfo(imageName:String,message:String,isHome:Bool = false) {
        homeImageView.image = UIImage(named: imageName)
        messageLabel.text = message
        if isHome {   //是首页
            turnImageView.hidden = false
            //开启动画
            startAnimation()
        }
        else{
            turnImageView.hidden = true
        }
    }
    
    //MARK : - 开启转动图片的动画
    func startAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //转动角度(360度)
        animation.toValue = 2 * M_PI
        
        //次数和时间
        animation.repeatCount = MAXFLOAT
        animation.duration = 10
        
        //切换视图的时候，动画完成默认为移除
        animation.removedOnCompletion = false
        //添加动画
        turnImageView.layer.addAnimation(animation, forKey: nil)
    }
    
    //3.懒加载控件
    
    //首页的房子
    private lazy var homeImageView : UIImageView = {
        let home = UIImageView()
        home.image = UIImage(named: "visitordiscover_feed_image_house")
        return home
    }()
    
    //蒙板
    private lazy var coverImageView : UIImageView = {
       
        let cover = UIImageView()
        cover.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        return cover
    }()
    
    //转动图片
    private lazy var turnImageView : UIImageView = {
       let turn = UIImageView()
        turn.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        return turn
    }()
    
    //label
    private lazy var messageLabel : UILabel = {
       let label = UILabel()
        //设置属性
        label.font = UIFont.systemFontOfSize(14)
        label.textColor = UIColor.darkGrayColor()
        label.numberOfLines = 0
        label.text = "..."
        label.textAlignment = .Center
        
        return label
    }()
    
    //注册按钮
    private lazy var registerBtn : UIButton = {
       
        let btn = UIButton()
        //设置按钮的标题和文字颜色
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        //设置背景
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        //添加按钮的点击事件
        btn.addTarget(self, action: "registerBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
    //登录按钮
    private lazy var loginBtn : UIButton = {
        let loginBtn = UIButton()
        //设置按钮属性
        loginBtn.setTitle("登录", forState: UIControlState.Normal)
        loginBtn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        //设置背景
        loginBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        //添加按钮的点击事件
        loginBtn.addTarget(self, action: "loginBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return loginBtn
    }()

    //MARK : -按钮的点击事件(私有方法系统找不到，就加@objc)
    @objc private func registerBtnClick() {
        print("注册")
        //调用代理方法，swift中不要判断是否有值
        delegate?.registerMethod()
    }
    
    @objc private func loginBtnClick() {
        print("登录")
        delegate?.loginMethod()
    }
}
